package com.luocj.elephant.demo.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.support.annotation.Nullable;
import android.support.annotation.XmlRes;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.luocj.elephant.demo.Bean.LineChartBean;
import com.luocj.elephant.demo.utils.Utils;

import java.util.ArrayList;

public class MyCurveView1 extends View {
    private static final String TAG = MyCurveView1.class.getSimpleName();
    private Paint mPaint;
    private int screenWidth;
    private ArrayList<LineChartBean> beans;
    private int width;
    private int height;
    private int xOrigin;
    private int yOrigin;
    private Point[] linePoints;
    private Path linePath;
    private boolean isBezierLine = true;
    private Paint bgPaint;

    public MyCurveView1(Context context) {
        this(context, null);
    }

    public MyCurveView1(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyCurveView1(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context mContext) {
        getDatas();
        linePoints = new Point[beans.size()];
        linePath = new Path();
        screenWidth = Utils.getScreenWidth(mContext) - Utils.dp2px(getContext(), 5);
        mPaint = new Paint();
        mPaint.setColor(Color.WHITE);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(Utils.dp2px(mContext, 6));
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setAntiAlias(true);

        bgPaint = new Paint();
        bgPaint.setAntiAlias(true);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        if (widthMode == MeasureSpec.EXACTLY) {
            width = widthSize;
        } else {
            width = Utils.getScreenWidth(getContext());
        }

        if (heightMode == MeasureSpec.EXACTLY) {
            height = heightSize;
        } else {
            height = Utils.dip2px(getContext(), 200);
        }

        setMeasuredDimension(width, height);

    }


    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        xOrigin = left + Utils.dp2px(getContext(), 5);
        yOrigin = bottom - Utils.dp2px(getContext(), 5);
        Log.i(TAG, "onLayout: " + "left -> " + left + ",top -> " + top + ",right -> " + right + ",bottom -> " + bottom);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        Log.i(TAG, "onSizeChanged: " + "w->" + w + ",h->" + h + ",oldw->" + oldw + ",oldh->" + oldh);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawColor(Color.rgb(20, 150, 250));
        canvas.translate(xOrigin, yOrigin);
        canvas.drawPoint(0, 0, mPaint);

        drawLine(canvas);

    }


    private void drawLine(Canvas canvas) {
        int setpWidth = screenWidth / (beans.size() - 1);
        int start = 0;
        Point pre = new Point();
        pre.set(start, -Utils.dip2px(getContext(), beans.get(0).getyAxis()));//坐标系从0,0默认在第四象限绘制
        linePoints[0] = pre;
//        linePath.moveTo(0,0);
        linePath.moveTo(pre.x, pre.y);
        for (int i = 1; i < beans.size(); i++) {
            LineChartBean lineChartBean = beans.get(i);
            //是否需要辅助线
//            canvas.drawLine(pre.x, 0, pre.x, (int) -(beans.get(i-1).getyAxis() / height * (Utils.dp2px(getContext(), height))), mPaint);
            Point next = new Point();
            next.set(start += setpWidth, (int) -(lineChartBean.getyAxis() / height * (Utils.dp2px(getContext(), height))));
            if (isBezierLine) {
                int cW = pre.x + setpWidth / 2;
                Point p1 = new Point();//控制点1
                p1.set(cW, pre.y);
                Point p2 = new Point();//控制点2
                p2.set(cW, next.y);
                linePath.cubicTo(p1.x, p1.y, p2.x, p2.y, next.x, next.y);//创建三阶贝塞尔曲线
            } else {
                linePath.lineTo(next.x, next.y);
            }
            pre = next;
            linePoints[i] = next;
        }
//        linePath.lineTo(pre.x, 0);
//        linePath.close();
//        LinearGradient hrLg = new LinearGradient(0,0, 0,-(beans.get(beans.size() - 1).getyAxis()), Color.BLUE,Color.BLUE, Shader.TileMode.CLAMP);
//        bgPaint.setShader(hrLg);
        canvas.drawPath(linePath, mPaint);

    }

    public ArrayList<LineChartBean> getDatas() {
        beans = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            LineChartBean lineChartBean = new LineChartBean();
            lineChartBean.setxAxis(i);
            lineChartBean.setyAxis((float) (Math.random() * 300));
            beans.add(lineChartBean);
        }
        return beans;
    }

    public void updata(LineChartBean newData) {
        beans.add(newData);
        beans.remove(0);
        linePath.reset();
        requestLayout();
        postInvalidate();
    }
}
